package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryString;
import com.itheima.entity.Result;
import com.itheima.pojo.ReserveOrder;
import com.itheima.service.ReserveOrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/reserveorder")
public class ReserveOrderController {

    @Reference
        private ReserveOrderService reserveOrderService;
        //分页查询
        @RequestMapping("/findPage")
        public PageResult findPage(@RequestBody QueryString queryString){
               return reserveOrderService.findPage(queryString);
    }
    //添加预约订单
    @RequestMapping("/add")
    public Result add(@RequestBody Map map, Integer[] setmealIds){
        try {
            reserveOrderService.add(map,setmealIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_RESERVEORDER_FAIL);
        }
        return new Result(true, MessageConstant.ADD_RESERVEORDER_SUCCESS);
    }
    @RequestMapping("/addOrderStatus")
    public Result addOrderStatus(@RequestBody ReserveOrder reserveOrder){   //id,orderStatus
        try {
            reserveOrderService.addOrderStatus(reserveOrder);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.UPDATE_SETMEAL_SUCCESS);
    }

    //编辑预约
    @RequestMapping("/edit")
    public Result edit(@RequestBody ReserveOrder reserveOrder){
        try{
            reserveOrderService.edit(reserveOrder);
            return new Result(true,"编辑预约成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"编辑预约失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            ReserveOrder reserveOrder = reserveOrderService.findById(id);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,reserveOrder);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    //删除预约
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            reserveOrderService.delete(id);
            return new Result(true,"删除预约数据成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除预约数据失败");
        }
    }
}
